﻿package app.demo.MyTouchApp{
 
    import flash.events.TUIO; // allows to connect to touchlib/tbeta
 
    import flash.display.*;
    import flash.events.*;
	import flash.events.MouseEvent;
    import flash.geom.*; // needed for curPt;
	import flash.display.SimpleButton;
	import flash.net.URLRequest;
	import flash.display.FrameLabel;
	import fl.controls.Label;
	import fl.controls.Button;
	import fl.controls.*;
	import fl.core.*;
	import flash.display.Sprite;
	import flash.text.*;
	import flash.system.fscommand;
	import flash.system.*;
	import flash.utils.getTimer;
	import flash.utils.Timer;
	import fl.transitions.easing.*;
	import flash.media.*;
	//import Math.abs;
 
     public class lettersGame extends MovieClip {
 
        private var color:uint = 0xff0000; // color of lines
        private var thickness:Number = 30; // thickness of lines
        private var blobs:Array = new Array(); // blobs we are currently interacting with
        private var lines:Array = new Array(); // array to hold the lines
		private var numButtonWidth:Number = 100;
		private var numButtonHeight:Number = 50;
		var enable:Boolean;
		
		var FirstPoint:Point;
		var SecondPoint:Point;
		
		
		var movie1:MovieClip = new MovieClip();
		var myTextFormat1:TextFormat = new TextFormat();
		
		private var avatars:Array = ["av11.png", "av22.png", "av33.png", "av44.png","av55.png","av66.png","av77.png","av88.png","av99.png","av100.png"];
		
		private var images:Array = ["darkBackground.png","av1.jpg", "lettersTitle.png","mute.png","unmute.png"];
		
		private var pics:Array = ["A.png","B.png","C.png","D.png","E.png","F.png"];
								  //,"8.png","9.png",
								  //"10.png","11.png","111.png","222.png","333.png", "1000.png","1100.png"];
		
		private var picsInHole:Array = ["AHNew.png","BHNew.png","CHNew.png","DHNew.png","EHNew.png","FHNew.png"];
		
		var positions:Array = new Array(pics.length*2);
		
		var iLoaders:Array = new Array (pics.length);
		var iLoaders2:Array = new Array (pics.length);
		
		var trialimg:Loader= new Loader();
		var hexa:Loader= new Loader();
		var triangle:Loader= new Loader();
		var star:Loader= new Loader();
		var circle:Loader= new Loader();
		var squaree:Loader= new Loader();
		
		//////////////////////////////////
		var heartNew:Loader= new Loader();
		var circleNew:Loader= new Loader();
		var triangleNew:Loader= new Loader();
		var squareNew:Loader= new Loader();
		var hexaNew:Loader= new Loader();
		var starNew:Loader= new Loader();
		//////////////////////////////////
	
		var imageBack:Loader = new Loader();
		var avatar:Loader = new Loader();
		var topicL:Label = new Label();
		var player:Label = new Label();
		var mainLoader:Loader = new Loader();
		var mainLoader1:Loader = new Loader();
		var mainLoader2:Loader = new Loader();
		var imageX:Loader = new Loader();
		var imageX1:Loader = new Loader();
		var topic:Loader = new Loader();
		var board:Loader = new Loader();
		var noSound:Boolean=false;
		
		public var buttonBack:Button;
		public var buttonExit:Button;
		public var buttonDone:Button;
		var skipp:Button = new Button();
		var skipp1:Button = new Button();
		var skipp2:Button = new Button();
		
		var square:Sprite = new Sprite();
		
		var myMaster:master;
		
		var skip2:Boolean = false;
		
		var errorSound:Sound;
		var correctSound:Sound;
		var mute:Loader  = new Loader();
		
		///////////////////Extra Variables//////////////////////
		//var played:Boolean=false;
		var once:Boolean=true;
  		
		var right:Loader = new Loader();
		var wrong:Loader = new Loader();
		
		var bmpimages:Array = new Array();
		
		var bound:Sprite= new Sprite();
		
		public var bringToFront:Boolean = true;
		public var mouseSelection = false;
		public var xdist:Number;
		public var ydist:Number;
		public var distance:Number;
		private var oldX:Number = 0;
		private var oldY:Number = 0;		
		public var lastClick:Number = 0;
		public var identity:Number = 0;
		public var clickRadius:Number = 50;		
		
		var pleft:int=6;
		var leftLabel:Label = new Label();
		var pictures:Array = new Array (10);
		
		var newTouchItem:Array = new Array();
		var doubleT:Timer = new Timer(500);
		////////////////////////////////////////////////////////
		
		public function lettersGame(m:master):void {
		
		TUIO.init(this,'localhost',3000,'',true);
        trace("MyTouchApp Initialized");
		myMaster=m;
		addChild(movie1);
		
		enable=false;
		
		//addEventListener(TouchEvent.MOUSE_DOWN, downEvent, false, 0, true); // run when finger down
        addEventListener(TouchEvent.MOUSE_UP, upEvent, false, 0, true); // run when finger up
        addEventListener(Event.ENTER_FRAME, updater, false, 0, true); // keep running
		//start();
		
		
		// mute buttons////
		
		
			mute.load(new URLRequest(images[4]));
			mute.name='unmute';
			mute.x=910;
			mute.y=580;
			mute.scaleX=0.3;
			mute.scaleY=0.3;
			mute.addEventListener(TouchEvent.MOUSE_DOWN,muteMethod);
		
		
		
		/////////////////////////////////////////////////
		//Saving the position of the shapes on the board,
		//in an array
		
		//CIRCLE at index 0:
		pictures[0]=new Array(2);
		pictures[0][0]=600;
		pictures[0][1]=340;
		
		//SQUARE at index 1:
		pictures[1]= new Array (2);
		pictures[1][0]=630;
		pictures[1][1]=180;
		
		//HEXA at index 2:
		pictures[2]= new Array (2);
		pictures[2][0]=480;
		pictures[2][1]=200;
		
		//TRIANGLE at index 3:
		pictures[3]= new Array (2);
		pictures[3][0]=230;
		pictures[3][1]=320;
		
		//HEART at index 4:
		pictures[4]= new Array (2);
		pictures[4][0]=300;
		pictures[4][1]=150;
		
		//STAR at index 5:
		pictures[5]= new Array (2);
		pictures[5][0]=400;
		pictures[5][1]=300;
		/////////////////////////////////////////////////
		
		////////////////////////////////////////////////////////
		//New images to be placed in the hole when the shape
		//fits correctly: // indices of the holes (top left)
		
		//CIRCLE:
		circleNew.load(new URLRequest(picsInHole[0]));
		circleNew.x=600;
		circleNew.y=340;
		//circleNew.rotationZ=;
		
		//SQUARE:
		squareNew.load(new URLRequest(picsInHole[1]));
		squareNew.x=630;
		squareNew.y=180;
		squareNew.rotationZ=15;
		
		//HEXA:
		hexaNew.load(new URLRequest(picsInHole[2]));
		hexaNew.x=480;
		hexaNew.y=200;
		hexaNew.rotationZ=-5;
		
		//TRIANGLE:
		triangleNew.load(new URLRequest(picsInHole[3]));
		triangleNew.x=230;
		triangleNew.y=320;
		triangleNew.rotationZ=4;
		
		//HEART:
		heartNew.load(new URLRequest(picsInHole[4]));
		heartNew.x=300;
		heartNew.y=150;
		heartNew.rotationZ=10;
		
		//STAR:
		starNew.load(new URLRequest(picsInHole[5]));
		starNew.x=400;
		starNew.y=300;
		//starNew.rotationZ=;
		/////////////////////////////////////////////////////////
		
		
		imageBack.load(new URLRequest(images[0]));
		imageBack.scaleX=1.4;
		//imageBack.scaleY=0.8;
		movie1.addChild(imageBack);
		
		myTextFormat1.font = "Kristen ITC";
		myTextFormat1.size = 70;
		
		
		topicL.text = "Transportation";
		topicL.width=600;
		topicL.height=300;
		topicL.x=260;
		topicL.visible=true;
		topicL.setStyle("textFormat", myTextFormat1);
		//movie1.addChild(topicL);
		
		topic.load(new URLRequest(images[2]));
		topic.x=350;
		topic.y=0;
		topic.scaleX=1.2;
		topic.scaleY=1.1;
		movie1.addChild(topic);
		
		
		buttonBack = new Button();
		buttonBack.label="BACK";
		buttonBack.width=100;
		buttonBack.height=40;
		//buttonBack.emphasized=true;
		buttonBack.addEventListener(TouchEvent.MOUSE_DOWN,backMethod);
		movie1.addChild(buttonBack);
		
		
		/*****************/
			
			/*imageX.load(new URLRequest(images[2]));
			imageX.x=975;
			imageX.y=16;
			imageX.alpha=0.0;
			imageX.addEventListener(TouchEvent.MOUSE_DOWN,disappear);
			movie1.addChild(imageX);
			
			
			imageX1.load(new URLRequest(images[2]));
			imageX1.x=975;
			imageX1.y=170;
			imageX1.alpha=0.0;
			imageX1.addEventListener(TouchEvent.MOUSE_DOWN,disappear1);
			movie1.addChild(imageX1);*/
			
			
			//mainLoader= new Loader();
			var url = new URLRequest("example4shapes.swf"); 
			mainLoader.load(url);
			mainLoader.y=60;
			mainLoader.x=10;
			mainLoader.name="mainTip1";
			mainLoader.addEventListener(TouchEvent.MOUSE_DOWN,moveTip);
			//mainLoader.addEventListener(MouseEvent.MOUSE_DOWN,methodTip);
			//mainLoader.addEventListener(TouchEvent.MOUSE_UP,upTip);
			movie1.addChild(mainLoader);
			
			
			skipp.label="Skip All Tips";
			skipp.addEventListener(TouchEvent.MOUSE_DOWN, skip3); 
			skipp.y=196;
			skipp.x=28;
			skipp.width=130;
			skipp.name="tip1";
			skipp.addEventListener(TouchEvent.CLICK,downEvent);
			movie1.addChild(skipp);
			
			//mainLoader= new Loader();
			var url1 = new URLRequest("example3shapes.swf"); 
			mainLoader1.load(url1);
			mainLoader1.y=240;
			mainLoader1.x=10;
			mainLoader1.name="mainTip2";
			mainLoader1.visible=false;
			mainLoader1.addEventListener(TouchEvent.MOUSE_DOWN,moveTip1);
			
			movie1.addChild(mainLoader1);
			
			
			skipp1.label="Skip All Tips";
			skipp1.addEventListener(TouchEvent.MOUSE_DOWN, skip4); 
			skipp1.y=435;
			skipp1.x=28;
			skipp1.width=130;
			skipp1.name="tip2";
			skipp1.visible=false;
			skipp1.addEventListener(TouchEvent.CLICK,downEvent);
			movie1.addChild(skipp1);
			
			//mainLoader= new Loader();
			var url2 = new URLRequest("example2shapes.swf"); 
			mainLoader2.load(url2);
			mainLoader2.y=475;
			mainLoader2.x=10;
			mainLoader2.name="mainTip3";
			mainLoader2.visible=false;
			movie1.addChild(mainLoader2);
			mainLoader2.addEventListener(TouchEvent.MOUSE_DOWN,moveTip2);
			
			skipp2.label="Skip All Tips";
			//skipp2.addEventListener(TouchEvent.MOUSE_DOWN, skip4); 
			skipp2.y=550;
			skipp2.x=28;
			skipp2.width=130;
			skipp2.name="tip3";
			skipp2.visible=false;
			movie1.addChild(skipp2);

			
		/*****************/
		
		buttonDone = new Button;
		buttonDone.x=30;
		buttonDone.y=650;
		buttonDone.width=120;
		buttonDone.height=40;
		buttonDone.label="SHOW TIPS";
		buttonDone.enabled=false;
		buttonDone.addEventListener(TouchEvent.MOUSE_DOWN,showTips);
		movie1.addChild(buttonDone);
		
		
		buttonExit = new Button;
		buttonExit.x=880;
		buttonExit.y=650;
		buttonExit.width=120;
		buttonExit.height=40;
		buttonExit.label="EXIT";
		buttonExit.addEventListener(TouchEvent.MOUSE_DOWN,closeApp);
		movie1.addChild(buttonExit);
		
		//this.addEventListener(TouchEvent.MOUSE_DOWN,moveTip);
		
		square.graphics.lineStyle(3,0x000000);
		square.graphics.beginFill(0xFFFFFF);
		square.graphics.drawRect(0,0,650,400);
		//square.graphics.endFill();
		square.x=200;
		square.y=100;
		//movie1.addChild(square);
		
		board.load(new URLRequest("board2.png"));
		board.x=180;
		board.y=100;
		movie1.addChild(board);
		
		iLoaders2[4] = new Loader();
		iLoaders2[4].load(new URLRequest("EHole.png"));
		iLoaders2[4].x=300;
		iLoaders2[4].y=150;
		iLoaders2[4].rotationZ=10;
		movie1.addChild(iLoaders2[4]);
		//trialimg.load(new URLRequest("heartHole.png"));
		//trialimg.x=300;
		//trialimg.y=150;
		//trialimg.rotationZ=30;
		//imageBack.scaleY=0.8;
		//movie1.addChild(trialimg);
		
		
		
		iLoaders2[2] = new Loader();
		iLoaders2[2].load(new URLRequest("CHole.png"));
		iLoaders2[2].x=480;
		iLoaders2[2].y=200;
		iLoaders2[2].rotationZ=-5;
		movie1.addChild(iLoaders2[2]);
		
		iLoaders2[3] = new Loader();
		iLoaders2[3].load(new URLRequest("DHole.png"));
		iLoaders2[3].x=230;
		iLoaders2[3].y=320;
		iLoaders2[3].rotationZ=4;
		movie1.addChild(iLoaders2[3]);
		
		iLoaders2[5] = new Loader();
		iLoaders2[5].load(new URLRequest("FHole.png"));
		iLoaders2[5].x=400;
		iLoaders2[5].y=300;
		//iLoaders2[5].rotationZ=-10;
		movie1.addChild(iLoaders2[5]);
		
		iLoaders2[1] = new Loader();
		iLoaders2[1].load(new URLRequest("BHole.png"));
		iLoaders2[1].x=630;
		iLoaders2[1].y=180;
		iLoaders2[1].rotationZ=15;
		movie1.addChild(iLoaders2[1]);
		
		iLoaders2[0] = new Loader();
		iLoaders2[0].load(new URLRequest("AHole.png"));
		iLoaders2[0].x=600;
		iLoaders2[0].y=340;
		//iLoaders2[0].rotationZ=10;
		movie1.addChild(iLoaders2[0]);
		
		avatar.load(new URLRequest(avatars[myMaster.getTheIndex()]));
		//avatar.x=20;
		//avatar.y=530;
		avatar.x=860;
		avatar.y=20;
		
		avatar.scaleX=1.1;
		avatar.scaleY=1.1;
		//avatar.addEventListener(TouchEvent.MOUSE_DOWN,dimMethod1);
		movie1.addChild(avatar);
		
		
		movie1.addChild(mute);
		
		//avatar.addEventListener(TouchEvent.MOUSE_DOWN,dimMethod1);
		
		var myTextFormat2:TextFormat = new TextFormat();
		myTextFormat2.font = "Kristen ITC";
		myTextFormat2.size = 20;
		myTextFormat2.color = 0xffffff;

		
				// to check name in keyboard//
		trace("i am here");
		//trace(myMaster.pageKeyboard.Text);
		player.text = myMaster.getTheText();
		//"Name";
		player.width=300;
		player.height=40;
		//player.x=45;
		//player.y=660;
		player.x=895;
		player.y=140;
		player.visible=true;
		player.setStyle("textFormat", myTextFormat2);
		movie1.addChild(player);
		
		
		loadimages();
		
		right.load(new URLRequest("correct.png"));
		right.visible=false;
		//movie1.addChild(right);
		
		wrong.load(new URLRequest("error.png"));
		wrong.visible=false;
		//movie1.addChild(wrong);
		
		//addimages();
				
		
		doubleT.addEventListener(TimerEvent.TIMER, doubleTTimer);
		
		
		}
		
		
		public function moveTip(evt:Event):void
		{
			
			if(mainLoader.visible==false)
			{
				var theX:int;
				var theY:int;
				
				theX=mainLoader1.x;
				theY=mainLoader1.y;
				
				//mainLoader1.y=60;
				//mainLoader1.x=10;
				
				mainLoader2.y=60;
				mainLoader2.x=10;
				
				bmpimages[3].x=bmpimages[3].x+1;
				bmpimages[3].x=bmpimages[3].x-1;
				
			}
			 //mainLoader.dispatchEvent(new MouseEvent(MouseEvent.DOWN));
			trace("in moveTip");
			var newEvent:MouseEvent = new MouseEvent(MouseEvent.CLICK);
			trace("New Event is: " + newEvent);
			//Alert.show("hi "+newEvent);
			mainLoader.addEventListener(MouseEvent.CLICK,methodTip);
			mainLoader.dispatchEvent(newEvent);
			
			mainLoader.addEventListener(TouchEvent.CLICK,downEvent);
			var newEvent2:TouchEvent = new TouchEvent(TouchEvent.CLICK);
			mainLoader.dispatchEvent(newEvent2);
			
			mainLoader1.addEventListener(TouchEvent.CLICK,downEvent);
			var newEvent3:TouchEvent = new TouchEvent(TouchEvent.CLICK);
			mainLoader1.dispatchEvent(newEvent3);
			
			mainLoader2.addEventListener(TouchEvent.CLICK,downEvent);
			//var newEvent4:TouchEvent = new TouchEvent(TouchEvent.CLICK);
			//mainLoader2.dispatchEvent(newEvent3);
		
			//dispatchEvent(new MouseEvent(MouseEvent.DOWN));
			
			 
		}
		
		
		public function moveTip1(evt:Event):void
		{
			
			if(mainLoader1.visible==false)
			{
				mainLoader2.y=100;
				mainLoader2.x=10;
				
				//bmpimages[3].x=bmpimages[3].x+1;
				//bmpimages[3].x=bmpimages[3].x-1;
			}
			 //mainLoader.dispatchEvent(new MouseEvent(MouseEvent.DOWN));
			trace("in moveTip");
			var newEvent:MouseEvent = new MouseEvent(MouseEvent.CLICK);
			trace("New Event is: " + newEvent);
			//Alert.show("hi "+newEvent);
			mainLoader1.addEventListener(MouseEvent.CLICK,methodTip);
			mainLoader1.dispatchEvent(newEvent);
			
			mainLoader1.addEventListener(TouchEvent.CLICK,downEvent);
			var newEvent2:TouchEvent = new TouchEvent(TouchEvent.CLICK);
			mainLoader1.dispatchEvent(newEvent2);
			
			
			mainLoader2.addEventListener(TouchEvent.CLICK,downEvent);
			//dispatchEvent(new MouseEvent(MouseEvent.DOWN));
			
			 
		}
		
		public function moveTip2(evt:Event):void
		{
			
			
			mainLoader2.addEventListener(TouchEvent.CLICK,downEvent);
			var newEvent3:TouchEvent = new TouchEvent(TouchEvent.CLICK);
			mainLoader2.dispatchEvent(newEvent3);
			//dispatchEvent(new MouseEvent(MouseEvent.DOWN));
			
			 
		}
		
		public function methodTip(evt:MouseEvent):void
		{
			trace("in methodTip");
			evt.updateAfterEvent();
		}
		
		public function upTip(evt:MouseEvent):void
		{
		if(mainLoader.visible==false)
			{
				mainLoader1.y=60;
				mainLoader1.x=10;
				
				mainLoader2.y=60;
				mainLoader2.x=10;
			}
			 evt.updateAfterEvent();
		}
		
		function loadimages()
		{
			var i:int=0;
			for(i=0; i<iLoaders.length; i++)
			{
				iLoaders[i] = new Loader();
				iLoaders[i].contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadImg);
				iLoaders[i].load(new URLRequest(pics[i]));
			}
		}
	
			
		
		function onLoadImg(evt:Event):void
		{
			if(once)
			{
			var i:int=0;
			for(i=0; i<iLoaders.length; i++)
			{
			
			var bitdata:BitmapData = new BitmapData(iLoaders[i].width, iLoaders[i].height, true, 0x00000000);
			bitdata.draw(iLoaders[i], new Matrix());
		
			var bitimage:Bitmap = new Bitmap(bitdata);
		
			bmpimages[i]=new tile();
			bmpimages[i].name=i;
	
			//bmpimages[i].addEventListener(TouchEvent.MOUSE_DOWN, bmpimages[i].downEvent);
			bmpimages[i].addEventListener(TouchEvent.MOUSE_DOWN, this.downEvent);									
			bmpimages[i].doubleClickEnabled = true;
			
			
			bmpimages[i].addChild(bitimage);		
			movie1.addChild(bmpimages[i]);
			
			
			
			}
			
			addimages();
			}
			once=false;
		}
		
		
		function addimages()
		{
			bound.graphics.lineStyle(3,0xFFFFFF);
 			  bound.graphics.drawRect(190,500,650,200);
   			movie1.addChild(bound);
			//bound.graphics.lineStyle(3,0x000000);
			//bound.graphics.drawRect(200,500,550,150);
			//movie1.addChild(bound);
			
			var i:int=0;
			var x1:Number = randomNumber(200,720);
			var y1:Number=510;
			var angle=randomNumber(0,20);
			var k:int=0;
			
			for(i=0; i<bmpimages.length; i++)
			{
				bmpimages[i].x=x1;
				bmpimages[i].y=y1;
				bmpimages[i].rotationZ=angle;
				
				positions[k]=bmpimages[i].x;
				k++;
				positions[k]=bmpimages[i].y;
				k++;
				
				x1=randomNumber(200,720);
				angle=randomNumber(0,20);
			}
			
			//bmpimages[1].x=250;
			//bmpimages[1].y=510;
			//bmpimages[1].rotationZ=45;
				
		}
		
		
function randomNumber(low:Number=NaN, high:Number=NaN):Number
{
  var low:Number = low;
  var high:Number = high;

  if(isNaN(low))
  {
    throw new Error("low must be defined");
  }
  if(isNaN(high))
  {
    throw new Error("high must be defined");
  }

  return Math.round(Math.random() * (high - low)) + low;
}
var gIndex:int=100;
var backpst:int=100;
var myTimerC:Timer = new Timer(2000);
var myTimerI:Timer = new Timer(2000);

function doubleTap(s:String, xc:int, yc:int)
{
	if(xc>179 && xc<851 && yc>99 && yc<421)
	{
		trace("xc and yc are");
		trace(xc);
		trace(yc);
	mainLoader2.visible=true; // why do we do that??
			
	var index:int = parseInt(s);
	gIndex=index;
	backpst=index*2;
	var totalx:int=bmpimages[index].x+bmpimages[index].width;
	var totaly:int=bmpimages[index].y+bmpimages[index].height;
	
	
	myTimerC.addEventListener(TimerEvent.TIMER, timerListenerC);
	myTimerI.addEventListener(TimerEvent.TIMER, timerListenerI);
	
	while(bmpimages[index].rotationZ>360)
	{
		bmpimages[index].rotationZ=bmpimages[index].rotationZ-360;
	}
	
	var diffang=Math.abs(bmpimages[index].rotationZ - iLoaders2[index].rotationZ);
	
	//if(index==0)
//	{
//		var centerX=(bmpimages[0].x+ bmpimages[0].width)/2;
//		var centerY=(bmpimages[0].y+ bmpimages[0].height)/2;
//		
//		var centerHX=(pictures[0][0]+ bmpimages[0].width)/2;
//		var centerHY=(pictures[0][1]+ bmpimages[0].height)/2;
//		
//		trace("Center Of Circle is:" + centerX + "," + centerY);
//		trace("Center Of Circle Hole is: " + centerHX + "," + centerHY);
//		diffxx=Math.abs(centerX-centerHX);
//		diffyy=Math.abs(centerY-centerHY);
//		trace("Differences between them is: " + diffxx +","+ diffyy);
//		
//		diffang=0;
//		
//	}
	//else
	//{
		var diffxx=Math.abs(bmpimages[index].x-pictures[index][0]);
		var diffyy=Math.abs(bmpimages[index].y-pictures[index][1]);
	//}
	
	
	
	trace("Coordinates in array");
			trace(pictures[0][0]);
			trace(pictures[0][1]);
			trace("Coordinates of x,y");
			trace(bmpimages[index].x);
			trace(bmpimages[index].y);
			trace("Angle of the image is");
			trace(bmpimages[index].rotationZ);
	
	if(diffxx<10 && diffyy<10 && diffang<10) //index==4 && diffang<10)
	{
		trace("difference in angle gowa el if is: " + diffang);
		
			//correct
			//right.load(new URLRequest("666.png"));
			
			bmpimages[index].x=pictures[index][0];
			bmpimages[index].y=pictures[index][1];
			
			trace("Fix image:");
			trace(bmpimages[index].x);
			trace(bmpimages[index].y);
			
			right.x=bmpimages[index].x;
			right.y=bmpimages[index].y;
			right.visible=true;
			
			//////////////////////////////////////////////////////////////
			//When the shape correctly fits then remove the current shape
			//and according to the index replace the new shapes that fits
			//and display the right sign image
			
			movie1.removeChild(bmpimages[index]);
			
			if (index==0)
				movie1.addChild(circleNew);
			else if (index==1)
				movie1.addChild(squareNew);
			else if (index==2)
				movie1.addChild(hexaNew);
			else if (index==3)
				movie1.addChild(triangleNew);
			else if(index==4)
				movie1.addChild(heartNew);
			else if (index==5)
				movie1.addChild(starNew);
			
			movie1.addChild(right);
			//////////////////////////////////////////////////////////
			///////////////////////////////////////////////////////////////////////////////////////////////
			///////Enable the pics to move again:
			var i:int=0;
			for( i=0; i<iLoaders.length;i++)
			{
				if(i!=index)
				 {
							bmpimages[i].addEventListener(TouchEvent.MOUSE_DOWN, bmpimages[i].downEvent);
							bmpimages[i].addEventListener(TouchEvent.MOUSE_DOWN, this.downEvent);
									
							bmpimages[i].alpha=1.0;
				}
			}
			/////////////////////////////////////////////////////////////////////////////////////////////////
			
			
			
			
			if(noSound==false)
			{
			correctSound= new Sound();
			correctSound.load(new URLRequest("woho.mp3"));
			correctSound.play();
			}
			
			myTimerC.start();
			bmpimages[index].removeEventListener(TouchEvent.MOUSE_DOWN, bmpimages[index].downEvent);
			bmpimages[index].removeEventListener(TouchEvent.MOUSE_DOWN, this.downEvent);									
			pleft--;
	}
	else
	{
		
		trace("difference in angle gowa el else is: " + diffang);
		//wrong.load(new URLRequest("error.png"));
		trace("Coordinates of wrong image");
		trace(bmpimages[index].x);
		trace(bmpimages[index].y);
		trace(bmpimages[index].rotationZ);
		
		
		wrong.x=bmpimages[index].x;
		wrong.y=bmpimages[index].y;
		//if(bmpimages[index].rotationZ<90 && bmpimages[index].rotationZ>0)
		//{
			//wrong.x=bmpimages[index].x - bmpimages[index].height * Math.sin(bmpimages[index].rotationZ);
		//}
		wrong.rotationZ=bmpimages[index].rotationZ;
		//if(bmpimages[index].rotationZ>180)
		//wrong.rotationZ=180;
		
		wrong.visible=true;
		trace("Coordinates of wrong");
		trace(wrong.x);
		trace(wrong.y);
		
		movie1.addChild(wrong);
		
		///////////////////////////////////////////////////////////////////////////////////////////////
			///////Enable the pics to move again:
			var i:int=0;
			for( i=0; i<iLoaders.length;i++)
			{
				if(i!=index)
				 {
							bmpimages[i].addEventListener(TouchEvent.MOUSE_DOWN, bmpimages[i].downEvent);
							bmpimages[i].addEventListener(TouchEvent.MOUSE_DOWN, this.downEvent);
									
							bmpimages[i].alpha=1.0;
				}
			}
			/////////////////////////////////////////////////////////////////////////////////////////////////
			
			
		
		
		if(noSound==false)
		{
		errorSound= new Sound();
		errorSound.load(new URLRequest("error.mp3"));
		errorSound.play();
		}
		
		myTimerI.start();
		
		//bmpimages[index].x=randomNumber(200,720);
		//bmpimages[index].y=550;
		
	}
	
	var myTextFormat3:TextFormat = new TextFormat();
	myTextFormat3.font = "Kristen ITC";
	myTextFormat3.size = 28;
	myTextFormat3.color = 0xffffff;
	
	leftLabel.text = "You have " + pleft + " pieces left";
	leftLabel.width=400;
	leftLabel.height=100;
	leftLabel.x=200;
	leftLabel.y=507;
	leftLabel.setStyle("textFormat", myTextFormat3);
	leftLabel.visible=true;
	//movie1.addChild(leftLabel);
	}
}

function timerListenerC (e:TimerEvent):void{
trace("Timer is Triggered");
//movie1.removeChild(right);
right.visible=false;

myTimerC.stop();
if(pleft==0)
	{
		myMaster.goToWellDone2();
	}
}

function timerListenerI (e:TimerEvent):void{
trace("Timer is Triggered");
trace("GIndex");
trace(gIndex);
//movie1.removeChild(wrong);
wrong.visible=false;


//bmpimages[gIndex].x=randomNumber(200,720);
//bmpimages[gIndex].y=510;
bmpimages[gIndex].x=positions[backpst];
backpst++;
bmpimages[gIndex].y=positions[backpst];
bmpimages[gIndex].rotationZ=randomNumber(0,20);
myTimerI.stop();
//bmpimages[gIndex].visible=false;
}


		
		function closeApp(event:TouchEvent):void
		{
			
			fscommand("quit");
			//System.exit(0);
		
		}
		
		public function wellDone(e:Event):void
		{
			//myMaster.goToWellDone();
		}		
		public function skip3(e:Event):void 
		{ 
		
		skipp.addEventListener(TouchEvent.CLICK,downEvent);
		var newEvent3:TouchEvent = new TouchEvent(TouchEvent.CLICK);
		skipp.dispatchEvent(newEvent3);
		
		
		enable=true;
		buttonDone.enabled=true;
		
		skip2=true; 
		movie1.removeChild(mainLoader); 
		if(skipp1.parent==movie1)
		{
			movie1.removeChild(mainLoader1);
			skipp1.visible=false;
			movie1.removeChild(skipp1);
		}
		if(mainLoader2.parent==movie1)
		{
			movie1.removeChild(mainLoader2);
			//skipp1.visible=false;
			//movie1.removeChild(skipp1);
		}
		//movie1.removeChild(mainLoader1);
		//movie1.removeChild(mainLoader2);
		skipp.visible=false;
		//skipp1.visible=false;
		movie1.removeChild(skipp);
		//movie1.removeChild(skipp1);
		}
		public function skip4(e:Event):void 
		{ 
		
		
		enable=true;
		buttonDone.enabled=true;
		
		skipp1.addEventListener(TouchEvent.CLICK,downEvent);
		var newEvent3:TouchEvent = new TouchEvent(TouchEvent.CLICK);
		skipp1.dispatchEvent(newEvent3);
		
		skip2=true; 
		
		movie1.removeChild(mainLoader1);
		skipp1.visible=false;
		movie1.removeChild(skipp1);
		//movie1.removeChild(mainLoader2);
		if(skipp.parent==movie1)
		{
		movie1.removeChild(mainLoader); 
		skipp.visible=false;
		movie1.removeChild(skipp);
		}
		if(mainLoader2.parent==movie1)
		{
		movie1.removeChild(mainLoader2); 
		//skipp.visible=false;
		//movie1.removeChild(skipp);
		}
		
		}
		
		/*public function disappear(e:Event):void
		{
			skipp.visible=false;
		}
		
		public function disappear1(e:Event):void
		{
			skipp1.visible=false;
		}*/
		
		
		function backMethod(event:TouchEvent):void
		{
			myMaster.backBoard3();
		}
		
		function muteMethod(event:TouchEvent):void
		{
			if(mute.name=='unmute')   // want to pause
			{
			//myMaster.mute();
			//mute2();
			noSound=true;
			mute.load(new URLRequest(images[3]));
			mute.name='mute';
			
			}
			else   // want to play again
			{
			//unmute2();
			noSound=false;
			mute.load(new URLRequest(images[4]));	
			mute.name='unmute';
			}
		}
		
		
		/*onClipEvent(enterFrame)
		{
			if(mainLoader.visible==false)
			{
				mainLoader1.y=10;
				mainLoader1.x=852;
			}
		}*/
		
		
		private function drawButtonState(rgb:uint):Sprite {
		var sprite:Sprite = new Sprite();
		//sprite.graphics.lineStyle(4,0x33621E,1);
		sprite.graphics.beginFill(rgb);
		sprite.graphics.drawRoundRect(((this.stage.
		stageWidth/2)-(numButtonWidth/2)),((this.
		stage.stageHeight/2)-(numButtonHeight/2)),
		numButtonWidth,numButtonHeight,10,10);
		return sprite;
		}
		
		    function updater(e:Event):void {
            for (var i:int = 0; i < blobs.length; i++) { 
                var tuioobj:TUIOObject=TUIO.getObjectById(blobs[i].id);
 
                // if not found, then it must have died..
                if (! tuioobj) {
                    removeBlob(blobs[i].id);
                } else if (parent != null) {
 
                    lines["line"+blobs[i].id].graphics.lineTo(tuioobj.x, tuioobj.y);
                }
            }
			if(mainLoader.visible==false)
			{
				mainLoader1.y=60;
				mainLoader1.x=10;
				
				mainLoader2.y=60;
				mainLoader2.x=10;
			}
        }
		
		function doubleTTimer (e:TimerEvent):void{
			
		}
		
		public function showTips(e:TouchEvent):void
		{
			if(enable==true)
			{
			skip2=false;
			
			if(mainLoader.parent==movie1)
				movie1.removeChild(mainLoader);
			if(mainLoader1.parent==movie1)
				movie1.removeChild(mainLoader1);
			if(mainLoader2.parent==movie1)
				movie1.removeChild(mainLoader2);
			
			var url = new URLRequest("example4shapes.swf"); 
			mainLoader.load(url);
			mainLoader.y=60;
			mainLoader.x=10;
			mainLoader.name="mainTip1";
			mainLoader.addEventListener(TouchEvent.MOUSE_DOWN,moveTip);
			movie1.addChild(mainLoader);
			
			
			skipp.label="Skip All Tips";
			skipp.addEventListener(TouchEvent.MOUSE_DOWN, skip3); 
			skipp.y=196;
			skipp.x=28;
			skipp.width=130;
			skipp.name="tip1";
			skipp.visible=true;
			skipp.addEventListener(TouchEvent.CLICK, downEvent); 
			movie1.addChild(skipp);
			
			var url1 = new URLRequest("example3shapes.swf"); 
			mainLoader1.load(url1);
			mainLoader1.y=240;
			mainLoader1.x=10;
			mainLoader1.name="mainTip2";
			//mainLoader1.visible=false;
			mainLoader1.addEventListener(TouchEvent.MOUSE_DOWN,moveTip1);
			
			movie1.addChild(mainLoader1);
			
			
			skipp1.label="Skip All Tips";
			skipp1.addEventListener(TouchEvent.MOUSE_DOWN, skip4); 
			skipp1.addEventListener(TouchEvent.CLICK, downEvent); 
			skipp1.y=435;
			skipp1.x=28;
			skipp1.width=130;
			skipp1.name="tip2";
			skipp1.visible=true;
			movie1.addChild(skipp1);
			
			//mainLoader= new Loader();
			var url2 = new URLRequest("example2shapes.swf"); 
			mainLoader2.load(url2);
			mainLoader2.y=475;
			mainLoader2.x=10;
			mainLoader2.name="mainTip3";
			//mainLoader2.visible=false;
			mainLoader2.addEventListener(TouchEvent.MOUSE_DOWN,moveTip2);
			movie1.addChild(mainLoader2);
			
			skipp2.label="Skip All Tips";
			//skipp2.addEventListener(TouchEvent.MOUSE_DOWN, skip4); 
			skipp2.y=550;
			skipp2.x=28;
			skipp2.width=130;
			skipp2.name="tip3";
			skipp2.visible=false;
			movie1.addChild(skipp2);


		}
			
			buttonDone.enabled=false;
			enable=false;
			
		}

		 public function downEvent(e:TouchEvent):void {
            var curPt:Point=parent.globalToLocal(new Point(e.stageX,e.stageY));
			trace ("Current point is");
			trace (curPt);
            //addBlob(e.ID, curPt.x, curPt.y);
            //e.stopPropagation();
			
			
			
			if(mainLoader.parent!=movie1 || mainLoader1.parent!=movie1 || mainLoader2.parent!=movie1 || skipp.parent!=movie1 ||
 skipp1.parent!=movie1 || skipp2.parent!=movie1)
			{
				buttonDone.enabled=true;
				enable=true;
			}
			
			
			if(skip2==false)
			{
			//mainLoader.visible=false;
			//skipp.visible=false;
			if(skipp.parent==movie1)
			{
			mainLoader1.visible=true;
			skipp1.visible=true;
			}
			else
			{
				
			mainLoader2.y=290;
			mainLoader2.x=10;
			
			mainLoader1.y=60;
			mainLoader1.x=10;
			skipp1.y=250;
			skipp1.x=28;
			mainLoader1.visible=true;
			skipp1.visible=true;
			
			
			
			}
			
			
			////////////// trying/////////////////////
			
					
					if(skipp.parent!=movie1 && skipp1.parent==movie1)
					{
					
					mainLoader2.y=290;
					mainLoader2.x=10;
					//mainLoader2.visible=true;
					}
					else if(skipp.parent!=movie1 && skipp1.parent!=movie1)
					{
					mainLoader2.y=60;
					mainLoader2.x=10;
					//mainLoader2.visible=true;
					}
					else if(skipp.parent==movie1 && skipp1.parent!=movie1)
					{
					mainLoader2.y=240;
					mainLoader2.x=10;
					//mainLoader2.visible=true;
					}
					
				
				
			
			/////////////////////////////////////////////
			
			}
			
			
			
			if(e.currentTarget.x>160 && e.currentTarget.x<760 && e.currentTarget.y>40 && e.currentTarget.y<700)
			{
				
			var index:int = parseInt(e.currentTarget.name);
			//if(e.currentTarget.x>200 && e.currentTarget.x<500)
//			{
//				bmpimages[index].removeEventListener(TouchEvent.MOUSE_DOWN, bmpimages[index].downEvent);
//			}
			
			///////////////////////////////////////////////////////////////////////////////////////////////
			//Once an image goes out of the specified border,
			//the rest of the images are dimmed and are disabled till the image already chosen is
			//placed correctly on the board..
			
			if(e.currentTarget.x<160 || e.currentTarget.x>760 || e.currentTarget.y<500 || e.currentTarget.y>700) //|| moveAgain==false)
			{
				var i:int=0;
				for(i=0; i<iLoaders.length;i++)
				{
					if(i!=index)
					{
						bmpimages[i].removeEventListener(TouchEvent.MOUSE_DOWN, bmpimages[i].downEvent);
						bmpimages[i].removeEventListener(TouchEvent.MOUSE_DOWN, this.downEvent);
						
						bmpimages[i].alpha=0.5;
					}
				}
				
				//Third Tip:
				if(skip2==false)
				{
					if(skipp.parent==movie1 && skipp1.parent==movie1)
					{
					mainLoader2.visible=true;
					}
					else if(skipp.parent!=movie1 && skipp1.parent==movie1)
					{
					
					mainLoader2.y=290;
					mainLoader2.x=10;
					mainLoader2.visible=true;
					}
					else if(skipp.parent!=movie1 && skipp1.parent!=movie1)
					{
					mainLoader2.y=60;
					mainLoader2.x=10;
					mainLoader2.visible=true;
					}
					else if(skipp.parent==movie1 && skipp1.parent!=movie1)
					{
					mainLoader2.y=240;
					mainLoader2.x=10;
					mainLoader2.visible=true;
					}
					
				}
				
				
			}
			else
			{
				var i:int=0;
				for( i=0; i<iLoaders.length;i++)
				{
					if(i!=index)
					{
						bmpimages[i].addEventListener(TouchEvent.MOUSE_DOWN, bmpimages[i].downEvent);
						bmpimages[i].addEventListener(TouchEvent.MOUSE_DOWN, this.downEvent);
								
						bmpimages[i].alpha=1.0;
					}
				}
			}
			/////////////////////////////////////////////////////////////////////////////////////////////
			
			
			
			if(e.stageX == 0 && e.stageY == 0)
				return;			
			
			var curPt:Point = parent.globalToLocal(new Point(e.stageX, e.stageY));									
			

			addBlob(e.ID, curPt.x, curPt.y);
			
			if(bringToFront)
				this.parent.setChildIndex(this, this.parent.numChildren-1);
			
				if(mouseSelection)
				{
					//this.alpha=0.5;				
				}
			
			//DoubleTap Gesture
			var tuioobj:TUIOObject = TUIO.getObjectById(e.ID);
			var localPt:Point = globalToLocal(new Point(tuioobj.x, tuioobj.y));
			
			xdist = localPt.x - oldX;
			ydist = localPt.y - oldY;			
			distance = Math.sqrt(xdist*xdist+ydist*ydist);
			oldX = localPt.x;
			oldY = localPt.y;		
			
			//trace(distance);
			
			if (parent.hitTestPoint(localPt.x,localPt.y) && distance <= clickRadius) {

				if (lastClick == 0) {
					lastClick = getTimer();
					trace("last click");
					trace(lastClick);
					identity=e.ID;
					doubleT.start();
					FirstPoint= parent.globalToLocal(new Point(e.stageX, e.stageY));
				} 
				else
				{
					SecondPoint= parent.globalToLocal(new Point(e.stageX, e.stageY));
					
					if(doubleT.running==true)
					{
						newTouchItem = TUIO.returnBlobs();
						var distanceBetween:Number=Point.distance(FirstPoint, SecondPoint);
						
						if(identity!=e.ID  && newTouchItem.length==1 && distanceBetween<10)
						{
							trace("The number of active blobs");
							trace(newTouchItem.length);
							lastClick = 0;
							trace("Double Tap");
							
							
							doubleTap(e.currentTarget.name, e.currentTarget.x, e.currentTarget.y);//perform doubleTap Function	
							
						}
						
					}
					else
					{
						lastClick = 0;
					}
					doubleT.stop();
				}
				
			}//end DoubleTap
				
			//e.stopPropagation();
			}
			
        }
		
		function clickHandler(evtObj:MouseEvent){
		trace("The " + evtObj.target.label + " button was clicked");
		}
 
        // When a finger stops touching, call removeBlob
        public function upEvent(e:TouchEvent):void {
            removeBlob(e.ID);
            e.stopPropagation();
        }
 
        // This takes the sent ID, x,y and puts it all in the blobs array
        // It then creats a new sprite, and sets the starting loc of the line
        // The line is put in an array called lines, so it can be called over and over by name
        // if the lines did not have unique names you could only draw one line at a time
        // so each line is named in the array after the id of the blob (EG line1280)
        function addBlob(id:Number, origX:Number, origY:Number):void {
            blobs.push( {id: id, origX: origX, origY: origY, myOrigX: x, myOrigY:y} );
 
            var line:Sprite = new Sprite();
            lines["line"+id] = line;
            //lines["line"+id].graphics.lineStyle(thickness, color); //<-- config above
            lines["line"+id].graphics.moveTo(origX, origY); //starting point of line to be drawn
            addChild(lines["line"+id]); // add line to stage
        }
 
        // when called, this function removes the blobs from the array
        // cycles through the blobs array until the blob is found by its ID, then it is removed.
        function removeBlob(id:Number):void {
            for (var i=0; i < blobs.length; i++) {
                if (blobs[i].id==id) {
                    blobs.splice(i, 1);
                    return;
                }
            }
        }
 
    }
}
		
		